from flask import Flask, render_template, json, request, redirect, session, jsonify, url_for
import plotly as py
import plotly.graph_objs as go
import pandas as pd

app = Flask(__name__)

@app.route("/")
def main():
    pyplt = py.offline.plot
    labels = ["男性", "女性"]
    values = [50.96, 49.04]
    data = [go.Pie(labels=labels, values=values)]
    layout = go.Layout(
        title="我国本科男女比例（%）",
    )
    fig = go.Figure(data=data, layout=layout)
    div = pyplt(fig, output_type='div', include_plotlyjs=False, auto_open=False, show_link=False)

    pyplt = py.offline.plot
    labels = ["男性", "女性"]
    values = [36, 64]
    data = [go.Pie(labels=labels, values=values)]
    layout = go.Layout(
        title="网络与新媒体男女比例（%）",
    )
    fig = go.Figure(data=data, layout=layout)
    div2 = pyplt(fig, output_type='div', include_plotlyjs=False, auto_open=False, show_link=False)

    context = {}
    context['graph'] = div
    context['graph2'] = div2
    return render_template('main.html', context=context)


@app.route("/page_second")
def page_second():
    pyplt = py.offline.plot
    labels = ["新媒体运营", "新媒体编辑", '产品经理', '市场经理', '短视频运营']
    data = [go.Pie(labels=labels)]
    layout = go.Layout(
        title="各个主流职位",
    )
    fig = go.Figure(data=data, layout=layout)
    div = pyplt(fig, output_type='div', include_plotlyjs=False, auto_open=False, show_link=False)

    import plotly.express as px
    data = pd.read_csv(r'data/salary.csv')
    fig2 = px.bar(
        data,
        x='Visualization_tools',
        y='salary'
    )
    div2 = pyplt(fig2, output_type='div', include_plotlyjs=False, auto_open=False, show_link=False)

    context = {}
    context['graph3'] = div
    context['graph4'] = div2
    return render_template('second.html', context=context)


@app.route("/page_final")
def page_final():
    return render_template('final.html')


if __name__ == "__main__":
    # 运行 Flask 系统实例
    app.run()
